/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms;

import java.sql.*;
import pacificlms.Verifiable.Database.*;
import pacificlms.Verifiable.*;
import pacificlms.Error.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Brian Hume
 */
public class DataHolder {
    private static ArrayList<CheckOutRule> CheckOutRuleList;
    private static ArrayList<MemberType> MemberTypeList;
    private static ArrayList<ResourceType> ResourceTypeList;
    private static HashMap<String, Setting> SettingMap;
    private static boolean Loaded = false;
    
    public static CheckOutRule GetCheckOutRule(String MemberType, String ResourceType)
    {
        if (!Loaded)
            return null;
        for (CheckOutRule COR: CheckOutRuleList)
        {
            if (COR.MemberType(null).equalsIgnoreCase(MemberType) && COR.ResourceType(null).equalsIgnoreCase(ResourceType))
            {
                return COR;
            }
        }
        return null;
    }
    
    public static ArrayList<MemberType> GetMemberTypeList()
    {
        if (!Loaded)
            return null;
        return MemberTypeList;
    }
    public static String[] GetMemberTypeStringArray()
    {
        if (!Loaded)
            return null;
        
        int Length = MemberTypeList.size();
        String[] Sary = new String[Length];
        for(int itr = 0; itr < Length; itr++)
        {
            Sary[itr] = MemberTypeList.get(itr).Type(null);
        }
        return Sary;
    }
    
    public static ArrayList<ResourceType> GetResourceTypeList()
    {
        if (!Loaded)
            return null;
        return ResourceTypeList;
    }
    public static String[] GetResourceTypeStringArray()
    {
        if (!Loaded)
            return null;
        
        int Length = ResourceTypeList.size();
        String[] Sary = new String[Length];
        for(int itr = 0; itr < Length; itr++)
        {
            Sary[itr] = ResourceTypeList.get(itr).Type(null);
        }
        return Sary;
    }
    
    public static Setting GetSetting(String Name)
    {
        if (!Loaded)
            return null;
        Setting S = SettingMap.get(Name);
        return S;
    }
    
    public static boolean LoadData(Connection Con)
    {
        Loaded = false;
        
        try
        {
            CheckOutRuleList = new ArrayList();
            MemberTypeList = new ArrayList();
            ResourceTypeList = new ArrayList();
            SettingMap = new HashMap<String, Setting>();
            
            Statement stmt = Con.createStatement();

            CheckOutRule CheckOutRule = new CheckOutRule();
            MemberType MemberType = new MemberType();
            ResourceType ResourceType = new ResourceType();
            Setting Setting = new Setting();

            String SQL;
            ResultSet RS;
            ResultSetMetaData RSM;
            
            SQL = CheckOutRule.FindIdenticalSQL();
            RS = stmt.executeQuery(SQL);
            RSM = RS.getMetaData();
            while(RS.next())
            {
                CheckOutRule COR = (CheckOutRule)VerifiableObject.CreateFromResultSet(CheckOutRule.class, RS, RSM);
                CheckOutRuleList.add(COR);
            }
                    
                    
            //SQL = MemberType.FindIdenticalSQL();
            //RS = stmt.executeQuery(SQL);
            //RSM = RS.getMetaData();
            //while(RS.next())
            //{
            //    MemberType MT = (MemberType)VerifiableObject.CreateFromResultSet(MemberType.class, RS, RSM);
            //    MemberTypeList.add(MT);
            //}
            //fixme this is bad, but no need to specifiy order.
            MemberType MT = new MemberType();
            MT.Type("nonmember");
            MemberTypeList.add(MT);
            MT = new MemberType();
            MT.Type("student");
            MemberTypeList.add(MT);
            MT = new MemberType();
            MT.Type("faculty");
            MemberTypeList.add(MT);
            MT = new MemberType();
            MT.Type("librarian");
            MemberTypeList.add(MT);

            SQL = ResourceType.FindIdenticalSQL();
            RS = stmt.executeQuery(SQL);
            RSM = RS.getMetaData();
            while(RS.next())
            {
                ResourceType RT = (ResourceType)VerifiableObject.CreateFromResultSet(ResourceType.class, RS, RSM);
                ResourceTypeList.add(RT);
            }

            SQL = Setting.FindIdenticalSQL();
            RS = stmt.executeQuery(SQL);
            RSM = RS.getMetaData();
            while(RS.next())
            {
                Setting S = (Setting)VerifiableObject.CreateFromResultSet(Setting.class, RS, RSM);
                SettingMap.put(S.Name(null), S);
            }
            
            Loaded = true;
        }
        catch(Exception Ex)
        {
            ErrorHandle.Display("Database Error", "Error connecting to database try closing the application and running it again\n" + Ex.toString(), Ex, true);
        }
        
        return Loaded;
    }
}
